#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include <time.h>
#include <stdlib.h>

osThreadId_t task_1_id;
osThreadId_t task_2_id;
osTimerId_t timer_id;

// 使用sleep()函数，每1秒输出一次信息
void Task1(void *arg){
    int time_cnt=0;
    while(1){
        printf("[Task1]Task1 is running.Now is %d\n",time_cnt);
        sleep(1);
        time_cnt++;
    }
}

// 软件定时器的回调函数，每3秒被调用一次
void Task2(void *arg){
    printf("[Task2]Task2 is running\n");
}


void timer_demo(){
    osThreadAttr_t attr1={
        .name="task1",
        .stack_size=1024*4,
        .priority=osPriorityNormal,
    };
    osThreadAttr_t attr2={
        .name="task2",
        .stack_size=1024*4,
        .priority=osPriorityNormal,
    };

    timer_id=osTimerNew(Task2,osTimerPeriodic,NULL,NULL);
    if(timer_id==NULL){
        printf("timer_id create failed");
    }
    else{
        printf("timer_id is %d",timer_id);
    }
    osTimerStart(timer_id,300);

    task_1_id=osThreadNew(Task1,NULL,&attr1);
    if(task_1_id==NULL){
        printf("[Task1]task_1_id create failed");
    }
    else{
        printf("[Task1]task_1_id is %d",task_1_id);
    }
}

SYS_RUN(timer_demo);
